package com.self.cgqcompose.ui.pages

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import com.self.cgqcompose.mock.MockJson
import com.self.cgqcompose.ui.components.HomeTopBar
import com.self.cgqcompose.ui.components.WebViewPage
import com.self.cgqcompose.ui.components.areatype.AreaCode9
import kotlinx.coroutines.launch
import kotlin.random.Random

@Composable
fun HomeScreen() {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White)
    ) {
//        Spacer(modifier = Modifier.size(40.dp))
        val context = LocalContext.current;
        val tabs = MockJson.loadMockDataTopTabs(context)
        val pagerState = rememberPagerState(initialPage = 2, pageCount = { tabs.tabs.size })
        var selectedIndex by rememberSaveable { mutableIntStateOf(2) }
        val pagerScope = rememberCoroutineScope()
        HomeTopBar(tabs, pagerState.currentPage, onItemSelected = {
            pagerScope.launch {
                pagerState.animateScrollToPage(it)
            }
        })
        HorizontalPager(
            state = pagerState,
            modifier = Modifier.fillMaxWidth(),
            beyondViewportPageCount = 0,
            verticalAlignment = Alignment.Top
        ) { page ->
            val randomColor = remember {
                Color(
                    red = Random.nextFloat(),
                    green = Random.nextFloat(),
                    blue = Random.nextFloat(),
                    alpha = 1f
                )
            }

            when(tabs.tabs[page].areaType) {
                9 -> {
                    Log.d("HomeScreen", "HomeScreen: ${tabs.tabs[page].name}")
                    val area9Bean = MockJson.loadMockDataArea9(context)
                    AreaCode9(area9Bean)
                }

                130 -> {
                    WebViewPage(tabs.tabs[page].path)
                }
                else -> {
                    Log.d("HomeScreen", "else HomeScreen: ${tabs.tabs[page].name}")
                    Text(
                        text = "Content for $page ${tabs.tabs[page].name}",
                        modifier = Modifier
                            .fillMaxSize()
                            .background(randomColor),
                        textAlign = TextAlign.Center
                    )
                }
            }



//            if(tabs.tabs[page].name == "精选"){
//                val area9Bean = MockJson.loadMockDataArea9(context)
//                AreaCode9(area9Bean)
//            }else{
//                Text(
//                    text = "Content for $page ${tabs.tabs[page].name}",
//                    modifier = Modifier
//                        .fillMaxSize()
//                        .background(randomColor),
//                    textAlign = TextAlign.Center
//                )
//            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun HomeScreenPreview() {
    HomeScreen()
}